package com.yangguang.huahui.ui.activitys;

import android.Manifest;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.map.GaoLocUtils;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.CryptoUtil;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.utils.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class LocationActivity extends MPermissionsActivity implements AMap.OnMapClickListener, AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter {
    private Unbinder mUnbinder;
    @BindView(R.id.mMapView)
    MapView mMapView;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.btn_add)
    Button btn_add;

    private AMap mAmap;
    private ArrayList<MarkerOptions> markerOptionlst = new ArrayList<>();//设置的
    private int type;//0=发送定位 2=显示定位 3=根据地址显示位置 4=根据经纬度限制位置
    private LatLng latLng;
    private double lat;
    private double lng;
    private String addr;

    private GeocodeSearch geocodeSearch;

    @OnClick({R.id.iv_back, R.id.btn_add})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_add:
                if (CommonUtil.isNull(DataInfo.LOC_ADDRESS)){
                    Toast.makeText(this, "定位还没有成功...请耐心等待", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(LocationActivity.this, ChatActivity.class);
                    intent.putExtra("address", DataInfo.LOC_ADDRESS);
                    intent.putExtra("latitude", DataInfo.LAT);
                    intent.putExtra("longitude", DataInfo.LNG);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        mUnbinder = ButterKnife.bind(this);
        mMapView.onCreate(savedInstanceState);
        tv_title.setText("位置信息");
//        requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0x0001);
        new GaoLocUtils(this);

        type = getIntent().getIntExtra("type", 0);
        lat = getIntent().getDoubleExtra("latitude",0);
        lng = getIntent().getDoubleExtra("longitude",0);
        addr = getIntent().getStringExtra("address");
        if (type == 1){
            latLng = new LatLng(DataInfo.LAT,DataInfo.LNG);
            btn_add.setVisibility(View.VISIBLE);
            btn_add.setText("发送");
        }else if (type == 2){
            btn_add.setVisibility(View.GONE);
            latLng = new LatLng(lat, lng);
        }
        init();
    }

    @Override
    public void permissionSuccess(int requestCode) {
        super.permissionSuccess(requestCode);
    }

    private void init(){
        if (mAmap == null){
            mAmap = mMapView.getMap();
            LogUtil.e("-------->amap" + mAmap, true);
            mAmap.moveCamera(CameraUpdateFactory.newLatLngZoom(DataInfo.HAIKOU, 13));
            UiSettings uiSettings = mAmap.getUiSettings();
            uiSettings.setScaleControlsEnabled(true);//显示比例尺控件
            uiSettings.setMyLocationButtonEnabled(false);
            registerListener();
        }
    }

    /**
     * 注册监听
     */
    private void registerListener() {
        mAmap.setOnMapClickListener(this);
        mAmap.setOnMarkerClickListener(this);
        mAmap.setOnInfoWindowClickListener(this);
        mAmap.setInfoWindowAdapter(this);
        addMarkersToMap();
    }

    private void addMarkersToMap() {
        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(latLng);
        markerOption.snippet(addr);
        markerOption.draggable(true).period(50);
        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(
                getResources(), R.mipmap.purple_pin)));
        // 将Marker设置为贴地显示，可以双指下拉看效果
        markerOption.setFlat(true);
        markerOptionlst.add(markerOption);
        List<Marker> markerList = mAmap.addMarkers(markerOptionlst, true);
        markerList.get(0).setSnippet(addr);
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onMapClick(LatLng latLng) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}
